package com.walter.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Projection;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.walter.dao.TeacherInfoDao;
import com.walter.domain.TeacherInfo;
import com.walter.utils.HibernateUtils;

public class TeacherInfoDaoImpl implements TeacherInfoDao {

	@Override
	public List<TeacherInfo> getAll() {
		// 1.获得session
		Session session = HibernateUtils.openSession();
		// 2.创建Criteria对象
		Criteria c = session.createCriteria(TeacherInfo.class);

		return c.list();
	}

	@Override
	public long getCount() {
		// 1.获得session
		Session session = HibernateUtils.openSession();
		// 2.创建Criteria对象
		Criteria c = session.createCriteria(TeacherInfo.class);

		c.setProjection(Projections.rowCount());
		long count = (long) c.uniqueResult();
		return count;
	}

	@Override
	public List<TeacherInfo> getAllByPage(int page) {
		// 1.获得session
		Session session = HibernateUtils.openSession();
		// 2.创建Criteria对象
		Criteria c = session.createCriteria(TeacherInfo.class);

		c.setFirstResult((page - 1) * 3);// 从第几条开始查
		c.setMaxResults(3);// 每页显示3条
		return c.list();
	}

	@Override
	public List<TeacherInfo> getByName(String name) {
		// 1.获得session
		Session session = HibernateUtils.openSession();
		// 2.创建Criteria对象
		Criteria c = session.createCriteria(TeacherInfo.class);
		// c.add(Restrictions.eq("t_name","%"+name+"%"));
		c.add(Restrictions.ilike("t_name", "%" + name + "%"));
		return c.list();
	}

	@Override
	public TeacherInfo getById(Long id) {
		// 1.获得session
		Session session = HibernateUtils.openSession();
		// 2.创建Criteria对象
		Criteria c = session.createCriteria(TeacherInfo.class);
		c.add(Restrictions.idEq(id));
		return (TeacherInfo) c.uniqueResult();
	}

}
